#include "user_list.h"

//初始化用户链表
user_node* init_list(void)
{
    user_node *users = calloc(1,sizeof(struct User_node));
    if(users == NULL)
    {
        perror("用户链表初始化失败");
        exit(0);
    }

    users->next = users;
    users->prev = users;
}

//显示当前在线用户
void show_list(user_node *users)
{
    user_node *p;
    printf("\n================================================\n");
    bool empty = true;
    for( p = users->next;p != users;p=p->next)
    {
        printf("用户%s(%u)[%s:%hu][%d]在线\n",p->user.username,
                        p->user.id,
                        inet_ntoa(p->user.addr.sin_addr),
                        ntohs(p->user.addr.sin_port),
                        p->user.connfd);
        empty = false;
    }
    if(empty)
    {
        printf("当前无用户\n");
    }
    printf("================================================\n");
}

//初始化用户节点
user_node *new_node(struct userinfo *info)
{
    user_node *new = calloc(1,sizeof(user_node));
    if(new == NULL)
    {
        perror("初始化用户节点失败");
        exit(0);
    }

    new->user = *info;
    new->next = new ;
    new->prev = new;
    return new;
}

//添加用户
void add_user(user_node *users,user_node *new_user)
{
    new_user->next = users;
    new_user->prev = users->prev;

    users->prev->next = new_user;
    users->prev = new_user;
}

//删除用户
void del_user(user_node *duser)
{

    duser->next->prev = duser->prev;
    duser->prev->next = duser->next;
    duser->next = NULL;
    duser->prev = NULL;
}



// 在用户链表中，找到指定地址的用户
user_node *find_out(user_node *users,struct sockaddr_in *paddr)
{
    user_node *p;
    
    for(p = users->next; p != users; p = p->next)
    {
        // printf("\r[%s:%hu]\n", inet_ntoa(p->user.addr.sin_addr), ntohs(p->user.addr.sin_port));
        if(p->user.addr.sin_addr.s_addr == paddr->sin_addr.s_addr)
        {
            // printf("\r找到[%s:%hu]\n", inet_ntoa(p->user.addr.sin_addr), ntohs(p->user.addr.sin_port));
            return p;
        }
    }
    return NULL;
}

//清空链表
void destory(user_node *users)
{
    free(users);
    users = NULL;
}

//根据id查询用户
user_node *find_user_by_id(user_node *users,unsigned int id)
{
    user_node *p;

    for(p = users->next; p != users; p = p->next)
    {
        if(p->user.id == id)
        {
            return p;
        }
    }
    return NULL;
}


/*
** 功能：从文件加载数据
** filename		文件名字
*/ 
void file_load_data(user_node* head, const char *filename)
{
	FILE *fp = fopen(filename, "r");
	if(fp == NULL)
	{
		perror("file_load_data fail");
		return ;
	}
	// 从文件读取数据，加载到链表
	struct userinfo tmp; 
	while( fread(&tmp, sizeof(struct userinfo), 1, fp) == 1 )
	{
        user_node *new = new_node(&tmp);
		add_user(head,new);
	}
	fclose(fp);
}

/*
** 功能：保存数据到文件
** filename		文件名字
*/ 
void file_save_data(user_node *head, const char *filename)
{
	FILE *fp = fopen(filename, "w");
	if(fp == NULL)
	{
		perror("file_save_data fail");
		return ;
	}
	
	// 把链表数据遍历，写入文件
	user_node *tmp = head->next;
	
	for( ; tmp != head; tmp = tmp->next)
	{
		fwrite(&tmp->user, sizeof(struct userinfo), 1, fp);
	}	
	
	fclose(fp);
}